package com.cwx.interfaces.rong360;

import com.alibaba.fastjson.JSONObject;
import com.cwx.interfaces.rong360.utils.RequestUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by code on 17-8-31.
 */
public class Rong360Interface {

    private static final String PRIVATEKEY = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBALg7lYx0GIg13Qvho4qlvtUjp7BbLnKUxDJN6SeMkNHbFE1BOrHTBqjML2mTyfXzUr1hTFytLIjxnWW7fPEfYyPG8QsdW+NapnRPgxGPPCPr6Yv0YUWO4V10A2xT/uBmTQ79q/YzZ0IHnqLqAaQI6TW50isuUa3fDi8mBMLuFKJRAgMBAAECgYAtnyuwZUtx54y+lXivK84u765l4J6G50oSfvKexZNTNGI5o06KnIxIno+bIGrhygiSNxb22DdwDg0BFUWiXDhrRlLxFLZ7nwDeN1idIg8fsiKLsqxIuvnrwY0L1e9lrSr/jDy8JFX0W8nB8NiUiV5GQhH+YlrsYvqlLCyoH/h0AQJBAPTLIiPEvN0QJwrkfSYhAHLmjdXpAUmnamZREl0SVbPCZ7SziJrFDQfkt/r762FZgS9+geAZuLseRIrMYxuaBkECQQDAqrT5B4lXCv8COngJswxb/8L2UxysSPBtcwsX+6znH0xr+A8vs7CQAuTBpb6uXO8StPMbkAuCgNDtwPFgfDgRAkBeemAMygvSjj9jVthudOJFdnJbkvhqQvEv2C5sOX1WSbvY5Ene4hqrZz0S41PvWKoheIQLyCGiJoZxw3yk1IeBAkEAjXSIHJK49kxfgE5XVZ+8wksc5GePFLsUsvcVPQ2OG77yCA9/buF6D7HgLIE5Z6FZ8h0ipKj0mmNjgjv3vIRXUQJBAKlXKNUFi0iSbqfjCQ82RftlOVT5mDPAD6b8iJo8yE5qURuvTilgaZT3FFMOD59/fMF8/ZHb+d2s/fZ4N4CXyQ4=";

    public static final String CALLBACK_URL = "http://www.e1oo.cn.ngrok.cc/rong360/callback";

    public static final String RETURN_URL_TOTB = "http://jmeter.ngrok.cc/wechat/totb";

    public static final String RETURN_URL_TOJD = "http://jmeter.ngrok.cc/wechat/tojdxx";
    /**
     * appid
     */
    public static final String app_id = "2010327";

    /**
     * 用户采集信息接口
     * <link>https://tianji.rong360.com/doc/crawl/tianjireport_collectuser.html</link>
     * @param params 接口请求的业务参数，授权参数请到<pre>ClientManager</pre>中进行修改
     * @return 返回结果的JSON对象，基本格式为
     * {"error": 200, "msg": "success", "tianji_api_tianjireport_collectuser_response": {}}
     * @throws Exception 当请求失败，或者返回的结果不是合法的json字符串时，会抛出异常
     */
    public static JSONObject collectUser(Map<String, Object> params) throws Exception {
        String method = "tianji.api.tianjireport.collectuser";
        String result = RequestUtil.request(method, params,app_id,PRIVATEKEY,false);
        if (result == null || result.length() == 0) {
            throw new Exception("Request tianji collectUser interface returns null");
        }
        JSONObject jsonRet = JSONObject.parseObject(result);
        if (jsonRet == null) {
            throw new Exception("Request tianji collectUser interface got a non-json result");
        }
        return jsonRet;
    }

    /**
     * 用户获取生成的报告详情接口
     * <link>https://tianji.rong360.com/doc/crawl/tianjireport_collectuser.html</link>
     * @param params 接口请求的业务参数，授权参数请到<pre>ClientManager</pre>中进行修改
     * @return 返回结果的JSON对象，基本格式为
     * {"error": 200, "msg": "success", "tianji_api_tianjireport_detail_response": {}}
     * @throws Exception 当请求失败，或者返回的结果不是合法的json字符串时，会抛出异常
     */
    public static JSONObject getReportDetail(Map<String, Object> params) throws Exception {
        String method = "tianji.api.tianjireport.detail";
        String result = RequestUtil.request(method, params,app_id,PRIVATEKEY,false);
        if (result == null || result.length() == 0) {
            throw new Exception("Request tianji collectUser interface returns null");
        }
        JSONObject jsonRet = JSONObject.parseObject(result);
        if (jsonRet == null) {
            throw new Exception("Request tianji collectUser interface got a non-json result");
        }
        return jsonRet;
    }
    /**
     * 用户获取学信网原始互联网报告接口
     * <link>https://tianji.rong360.com/doc/crawl/tianjireport_collectuser.html</link>
     * @param params 接口请求的业务参数，授权参数请到<pre>ClientManager</pre>中进行修改
     * @return 返回结果的JSON对象，基本格式为
     * {"error": 200, "msg": "success", "tianji_api_tianjireport_detail_response": {}}
     * @throws Exception 当请求失败，或者返回的结果不是合法的json字符串时，会抛出异常
     */
    public static JSONObject getChsi(Map<String, Object> params) throws Exception {
        String method = "wd.api.chsi.getData";
        String result = RequestUtil.request(method, params,app_id,PRIVATEKEY,false);
        if (result == null || result.length() == 0) {
            throw new Exception("Request tianji collectUser interface returns null");
        }
        JSONObject jsonRet = JSONObject.parseObject(result);
        if (jsonRet == null) {
            throw new Exception("Request tianji collectUser interface got a non-json result");
        }
        return jsonRet;
    }
    public static void main(String[] args) throws Exception {
        Map<String,Object> params = new HashMap<>();
        params.put("type","mobile");
        params.put("platform","web");
        params.put("name","李哲");
        params.put("phone","18678542601");
        params.put("idNumber","37292219951029391x");
        params.put("userId","2");
        params.put("outUniqueId","2");
        params.put("return_url","http://dunpaizx.98zhengxin.com/rong360/callback");
        params.put("notifyUrl","http://dunpaizx.98zhengxin.com/rong360/callback");
        System.out.println(Rong360Interface.collectUser(params));
//        Map<String,Object> params = new HashMap<>();
//        params.put("userId","1");
//        params.put("outUniqueId","1");
//        params.put("reportType","html");
//        System.out.println(Rong360Interface.getReportDetail(params));
    }
}

//    字段	类型	必需	描述
//    type	string	是	jd,mobile,ec,ibank,chsi,insure,fund,mobile_crawl，具体释义见下表
//        platform	string	是	请求的平台，可取值范围是api,web
//        name	string	是	姓名
//        phone	string	是	手机号(运营商报告请与传给抓取服务的手机号一致)
//        idNumber	string	是	身份证号码
//        userId	string	是	调用方生成的用户ID（调用方定义）
//        outUniqueId	string	是	调用方生成的会话唯一标识id，建议使用流水号生成（调用方定义）
//        notifyUrl	string	是	服务器异步通知页面路径